package video;

import net.cl.tools.json.JsonArray;
import net.cl.tools.json.JsonObject;
import net.cl.utils.HttpUtil;

/**
 * http://tv.sohu.com/20110417/n305949099.shtml
 * 
 * @author caol64@gmail.com
 * 
 */
public class Sohu {

	private static final String INDEX_PREFIX = "http://hot.vrs.sohu.com/vrs_flash.action?vid=";
	private static final String INDEX_PREFIX1 = "http://my.tv.sohu.com/videinfo.jhtml?outType=3&m=view&id=";
	private static final String INDEX_PREFIX2 = "http://my.tv.sohu.com/playlist.jhtml?outType=3&m=get&id=";
	private static final String VIEW_COUNT_URL = "http://count.vrs.sohu.com/count/stat.do?videoId=";
	private static final String VIEW_COUNT_URL1 = "http://vstat.my.tv.sohu.com/dostat.do?method=setVideoPlayCount&v=";
	private static final String S1 = "http://v.blog.sohu.com/u/vw/";

	public String getVideoSource(String videoKey) {
		String vid = videoKey;
		String index = INDEX_PREFIX;
		JsonObject jsObj = JsonObject.connect(index + vid);
		// video.setUri(jobj.getString("url"));
		String allot = jsObj.getString("allot");
		jsObj = jsObj.getJsonObject("data");
		// if (jsObj.containsKey("totalDuration"))
		// video.setDuration(jobj.getString("totalDuration"));
		StringBuilder sb = new StringBuilder();
		if (jsObj.containsKey("clipsURL")) {
			JsonArray jarr = jsObj.getJsonArray("clipsURL");
			for (int i = 0; i < jarr.size(); i++) {
				String url = jarr.getString(i);
				url = url.replace("data.vod.itc.cn", allot + "/?prot=2&file=");
				String file = jsObj.getJsonArray("su").getString(i);
				url = url + "&new=" + file + "&t=" + Math.random();
				String result = HttpUtil.getResponseString(url);
				String[] tmp0 = result.split("\\|");
				if (!tmp0[4].equals("1"))
					System.out.println("not yet happen.");
				if (i > 0)
					sb.append("[=]");
				sb.append(tmp0[0] + file.substring(1) + "?key=" + tmp0[3]);
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		Sohu k = new Sohu();
		System.out.println(k.getVideoSource("295673"));
	}

}
